package drukarnia.view;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import drukarnia.controller.UserManager;
import drukarnia.model.User;

@WebServlet("/RegisterAction")
public class RegisterAction extends HttpServlet {
        private static final long serialVersionUID = 1L;
       
    public RegisterAction() {
        super();
    }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                response.setContentType("text/html; charset=UTF-8");
                User user = new User();
                UserManager linkController = new UserManager();
                String message = "";
                try {
                        BeanUtils.populate(user, request.getParameterMap());
                        if(linkController.checkLoginIMail(user.getMail())){
                                linkController.register(user);
                                message = "Zarejestrowano!";
//                                request.setAttribute("login", user.getLogin());
//                                request.setAttribute("password", user.getPassword());
                                HttpSession session = request.getSession();
                                session.setAttribute("mail", user.getMail());
        						session.setAttribute("password", user.getPassword());
                                
                        }else{
                                message = "Podany login lub e-mail jest zajety, podaj inny";
                        }
                        String x = "message="+message;
                       
						
                        response.sendRedirect("checkLoginGR.jsp?afterRegister=true&" + x);
                       // checkLoginGR.jsp?afterRegister=true&" + x);
//                        RequestDispatcher dispatcher = request.getRequestDispatcher("checkLoginGR.jsp?afterRegister=true&" + x);
//    					dispatcher.forward(request, response);
                        
                } catch (IllegalAccessException e) {
                        e.printStackTrace();
                } catch (InvocationTargetException e) {
                        e.printStackTrace();
                }
        }

}